package es.daw.ejercicio3.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.daw.ejercicio3.dao.ContactDao;
import es.daw.ejercicio3.dao.domain.Contact;

/**
 * Servlet implementation class NewContactAction
 */
public class NewContactAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewContactAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Contact contacto = new Contact();
		contacto.setNombre(request.getParameter("name"));
		contacto.setEmail(request.getParameter("email"));
		contacto.setApellidos(request.getParameter("surname"));
		contacto.setTelefono(request.getParameter("telephone"));
		contacto.setDireccion(request.getParameter("address"));
		contacto.setCodPostal(request.getParameter("zipcode"));
		contacto.setProvincia(request.getParameter("province"));
		
		ContactDao contactDao = new ContactDao();
		contactDao.saveContact(contacto);
		
		request.setAttribute("mensaje", "El contacto se ha guardado correctamente");
		
	}

}
